<?php
$link = mysqli_connect("localhost","root","root","2116");
//获取数据库的内容
$sql = "SELECT * FROM students";
$res =  mysqli_query($link,$sql);
// $one = mysqli_fetch_object($res);
// print_r($one);
// die;
$arr = [];
while($one=mysqli_fetch_assoc($res)){
  $arr[]=$one;
}

?>




<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <link rel="stylesheet" href="./index.css" />
  </head>

  <body>
    <div class="wrap">
      <div class="title">学生系统</div>
      <div id="form">
        <p>姓名<input id="username" type="text" value="张三123" /></p>
        <p>年龄<input id="age" type="text" value="21" /></p>
        <p>
          性别
          <select id="gender">
            <option>男</option>
            <option>女</option>
            <option>你猜</option>
          </select>
        </p>
        <p>
          <input type="button" value="添加" id="addBtn" />
          <input type="button" value="排序" id="sortBtn" />
        </p>
      </div>
      <table id="table">
        <thead>
          <tr>
            <th>姓名</th>
            <th>年龄</th>
            <th>性别</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          <?php foreach($arr as $v){ ?>
            <tr>
            <td><?php echo $v ['username']; ?></td>
            <td><?php echo $v ['age']; ?></td>
            <td><?php echo $v ['gender']; ?></td>
            <td><a href="./del.php?id=<?php echo $v['id']; ?>">删除</a></td>
          </tr>
         <?php } ?>
          
        </tbody>
      </table>
    </div>

    <script>
      //1、建立form表单： method  get  2、from method post 3、可以js 跳转php页面 同时带参数
      var usernameEle = document.querySelector("#username");
      var ageEle = document.querySelector("#age");
      var genderEle = document.querySelector("#gender");
      var addBtn = document.querySelector("#addBtn");

      addBtn.onclick = function () {
        let username = usernameEle.value;
        let age = ageEle.value;
        let gender = genderEle.value;
        //console.log(username,age,gender);

        window.location.href = `./add.php?username=${username}&age=${age}&gender=${gender}`;
      };

      // // 1.获取元素
      // var usernameEle = document.querySelector("#username");
      // var ageEle = document.querySelector("#age");
      // var genderEle = document.querySelector("#gender");
      // var addBtn = document.querySelector("#addBtn");
      // var tbodyEle = document.querySelector("tbody");
      // // 立刻获取了input里的值 ，放在username变量里存起来了 ，后面就不会改变了；
      // // var username = usernameEle.value;

      // // console.log(username);
      // // 点击添加的时候获取 学生的信息；
      // addBtn.onclick = function () {
      //   var username = usernameEle.value;
      //   var age = ageEle.value;
      //   var gender = genderEle.value;
      //   // console.log(username,age,gender);
      //   // 模板字符串 可以支持换行；
      //   // var str = "<div>"+
      //   //                 "<span>1</span>"+
      //   //                 "<span>2</span>"+
      //   //           "</div>";
      //   tbodyEle.innerHTML += `<tr>
      //                                   <td>${username}</td>
      //                                   <td>${age}</td>
      //                                   <td>${gender}</td>
      //                                   <td><button>删除</button></td>
      //                             </tr>`;

      //   //    获取删除按钮，一定是在删除按钮生成之后；
      //   addDeleEvent();
      // };

      // function addDeleEvent() {
      //   var btns = document.querySelectorAll("td button");
      //   btns.forEach(function (btn) {
      //     btn.onclick = function () {
      //       // console.log("点击了删除按钮")
      //       this.parentNode.parentNode.remove();
      //     };
      //   });
      // }
    </script>
  </body>
</html>
